/*
 * Copyright (c) 2011. c.mueller
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * The Software shall be used for Good, not Evil.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package de.altitude.cimuelle;

import org.apache.log4j.Logger;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * User: cybernaut
 * Date: 18.09.11
 * Time: 00:15
 */
public final class SendMail {
    private static final Logger LOG = Logger.getLogger("SendMail");
    private static final SendMail INSTANCE = new SendMail();

    public static SendMail getINSTANCE() {
        return INSTANCE;
    }

    private SendMail() {
    }

    public static void postMail(final String recipient,
                                final String subject,
                                final String message,
                                final String from)
            throws MessagingException {
        final Properties props = new Properties();
        props.put("mail.smtp.host", Config.getInstance().getConfig("smtpHost", "localhost"));
        props.put("mail.smtp.port", Config.getInstance().getConfig("smtpPort", "25"));
        final Session session = Session.getDefaultInstance(props);
        final Message msg = new MimeMessage(session);
        final InternetAddress addressFrom = new InternetAddress(from);
        msg.setFrom(addressFrom);
        final InternetAddress addressTo = new InternetAddress(recipient);
        msg.setRecipient(Message.RecipientType.TO, addressTo);
        final InternetAddress addressCC = new InternetAddress(Config.getInstance().getConfig("adminMail", ""));
        if ("true".equals(Config.getInstance().getConfig("mailCC", "false")) && !addressTo.equals(addressCC)) {
            msg.addRecipient(Message.RecipientType.BCC, addressCC);
        }
        msg.setSubject(subject);
        msg.setContent(message, "text/plain");
        LOG.info("Trying to send mail.");
        LOG.debug("recipient: " + recipient);
        LOG.debug("subject:   " + subject);
        LOG.debug("body:      " + message);
        LOG.debug("from:      " + from);
        Transport.send(msg);
    }

}
